package com.ews.onlineexchange.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@ApplicationScoped
public class EnumUtils {
	
	private static final String DEFAULT_PACKAGE = "com.ews.onlineexchange.model";
	
	public Object[] getEnumArray(String enumName) {
		enumName = DEFAULT_PACKAGE.concat(".").concat(enumName);
		try {
			Class clazz = Class.forName(enumName);
			Object[] values = clazz.getEnumConstants();
			return values;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Object getEnum(String enumClassName, String enumName) {
		enumClassName = DEFAULT_PACKAGE.concat(".").concat(enumClassName);
		try {
			Class clazz = Class.forName(enumClassName);
			Object[] values = clazz.getEnumConstants();
			for (int i = 0; i < values.length; i++) {
				Method method = clazz.getMethod("name", new Class[]{});
				String name = (String) method.invoke(values[i], new Object[]{});
				if (name.equals(enumName)) {
					return values[i];
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
}
